package org.wk.concurrent.app1;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 这个并行算法是通过将FutureTask放入容器中，然后遍历该容器，获得结果。
 */
public class Client {
	public static void main(String[] args) throws InterruptedException,
			ExecutionException, TimeoutException {
		FetchList fetchList = new FetchList();
		FetchedList fetchedList = new FetchedList();
		List<File> results = new ArrayList<File>();
		ExecutorService exector = Executors.newFixedThreadPool(30);
		List<FutureTask<File>> tasks = new ArrayList<FutureTask<File>>();

		for (int i = 0; i < fetchList.size(); i++) {
			Crawler crawler = null;
			String url = fetchList.getUrl();

			// 检测这个url是否已经抓取过
			if (fetchedList.validate(url)) {
				crawler = new Crawler(url);
				fetchedList.addUrl(url);
			}

			FutureTask<File> task = new FutureTask<File>(crawler);
			if (!exector.isShutdown()) {
				exector.submit(task);
				tasks.add(task);
			}
		}

		for (FutureTask<File> t : tasks) {
			File file = t.get(3, TimeUnit.SECONDS);
			results.add(file);
		}

		exector.shutdown();

		for (File f : results) {
			System.out.println(f.getName());
		}
	}
}
